package com.woniu.owner.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.woniu.common.ResponseData;
import com.woniu.owner.entity.LoginByWeixinForm;
import com.woniu.owner.entity.MaintenanceInfo;
import com.woniu.owner.entity.ScmspOwner;
import com.woniu.owner.entity.ScmspOwnerHouse;
import com.woniu.owner.service.MaintenanceInfoService;
import com.woniu.owner.service.ScmspOwnerHouseService;
import com.woniu.owner.service.ScmspOwnerService;
import com.woniu.owner.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@RestController
@RequestMapping("/own")
public class OwnerController {

    @Autowired
    private ScmspOwnerService scmspOwnerService;
    @Autowired
    private ScmspOwnerHouseService scmspOwnerHouseService;
    @Autowired
    private MaintenanceInfoService maintenanceInfoService;
    @Autowired
    private UserService userService;
    @PostMapping("/queryOwner")
    public ResponseData<?> queryOwner(@RequestParam("phoneNum") String phoneNum){
        ScmspOwner scmspOwner = scmspOwnerService.lambdaQuery().eq(ScmspOwner::getTelephoneNumber, phoneNum)
                .one();
        return new ResponseData<>().ok(scmspOwner);
    }

    /**
     * 查询空房间
     * @return
     */
    @GetMapping("/queryKong")
    public ResponseData<?> queryKong(){
        List<ScmspOwnerHouse> list = scmspOwnerHouseService.lambdaQuery().eq(ScmspOwnerHouse::getStatus, "0")
                .list();
        return new ResponseData<>().ok(list);
    }
    /**
     * 选择房屋
     */
    @PostMapping("/selectHouse")
    public ResponseData<?> selectHouse(@RequestParam("phoneNum") String phoneNum, @RequestParam("id") Integer id){
        ScmspOwnerHouse house = scmspOwnerHouseService.lambdaQuery().eq(ScmspOwnerHouse::getId, id).one();
        house.setStatus(1);
        house.setPhoneNumber(phoneNum);
        scmspOwnerHouseService.updateById(house);
        return new ResponseData<>().ok();
    }
    /**
     * 查询我的房子
     */
    @PostMapping("/queryMyHouse")
    public ResponseData<?> queryMyHouse(@RequestParam("phoneNum") String phoneNum){
        List<ScmspOwnerHouse> list = scmspOwnerHouseService.lambdaQuery().eq(ScmspOwnerHouse::getPhoneNumber, phoneNum).list();
        return new ResponseData<>().ok(list);
    }
    /**
     * 我的故障上报
     */
    @PostMapping("/queryMyFault")
    public ResponseData<?> queryMyFault(@RequestParam("phoneNum") String phoneNum){
        List<MaintenanceInfo> list = maintenanceInfoService.lambdaQuery().eq(MaintenanceInfo::getPhone, phoneNum).list();
        return new ResponseData<>().ok(list);
    }

    @ApiOperation("通过微信登录")
    @PostMapping("/login/weixin")
    public ResponseData<?> loginByWeixin(@RequestBody LoginByWeixinForm form) {
        return userService.loginByWeixin(form);
    }


}